$("#mfilegzhimg").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 1,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=gzhimg',
    allowedFileExtensions: ["jpg", "jpeg", "png"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidgzhimg").val(data.response.data);
})

$("#mfilekefuimg").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 1,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=kefuimg',
    allowedFileExtensions: ["jpg", "jpeg", "png"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidkefuimg").val(data.response.data);
})

var xiangqing, xiangqing2;
$("#mfilexiangqing").fileinput({
    showPreview: false,
    showUpload: true,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=xiangqing',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    $("#hidxiangqing").val($("#hidxiangqing").val().length == 0 ? data.response.data : $("#hidxiangqing").val() + ";" + data.response.data);
    xiangqing = createsort('xiangqing')
}).on("filebatchuploadsuccess", function (event, data, previewId, index) {
    $("#hidxiangqing").val($("#hidxiangqing").val().length == 0 ? data.response.data : $("#hidxiangqing").val() + ";" + data.response.data);
    xiangqing = createsort('xiangqing')
}).on('filedeleted', function (event, key, jqXHR, data) {
    $("#hidxiangqing").val(filedeleted($("#hidxiangqing").val(), key));
    xiangqing = createsort('xiangqing')
})

var photos, photos2;
$("#mfilephotos").fileinput({
    showPreview: false,
    showUpload: true,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=photos',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    $("#hidphotos").val($("#hidphotos").val().length == 0 ? data.response.data : $("#hidphotos").val() + ";" + data.response.data);
    photos = createsort('photos')
}).on("filebatchuploadsuccess", function (event, data, previewId, index) {
    $("#hidphotos").val($("#hidphotos").val().length == 0 ? data.response.data : $("#hidphotos").val() + ";" + data.response.data);
    photos = createsort('photos')
}).on('filedeleted', function (event, key, jqXHR, data) {
    $("#hidphotos").val(filedeleted($("#hidphotos").val(), key));
    photos = createsort('photos')
})

var zhengshu, zhengshu2;
$("#mfilezhengshu").fileinput({
    showPreview: false,
    showUpload: true,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=zhengshu',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    $("#hidzhengshu").val($("#hidzhengshu").val().length == 0 ? data.response.data : $("#hidzhengshu").val() + ";" + data.response.data);
    zhengshu = createsort('zhengshu')
}).on("filebatchuploadsuccess", function (event, data, previewId, index) {
    $("#hidzhengshu").val($("#hidzhengshu").val().length == 0 ? data.response.data : $("#hidzhengshu").val() + ";" + data.response.data);
    zhengshu = createsort('zhengshu')
}).on('filedeleted', function (event, key, jqXHR, data) {
    $("#hidzhengshu").val(filedeleted($("#hidzhengshu").val(), key));
    zhengshu = createsort('zhengshu')
})

$("#mfileinfo").fileinput({
    showPreview: true,
    showUpload: true,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=goods&f=info',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidinfo").val($("#hidinfo").val().length == 0 ? data.response.data : $("#hidinfo").val() + ";" + data.response.data);
})

$("#mfilelogo").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 9,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=logo',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidlogo").val(data.response.data);
})

$("#mfilezhizhao").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 1,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=zhizhao',
    allowedFileExtensions: ["jpg", "jpeg", "png"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidzhizhao").val(data.response.data);
})

$("#mfilexukezheng").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 1,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=xukezheng',
    allowedFileExtensions: ["jpg", "jpeg", "png"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidxukezheng").val(data.response.data);
})

$("#mfilepic").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 1,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=pic',
    allowedFileExtensions: ["jpg", "jpeg", "png"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidpic").val(data.response.data);
})

$("#gmfile").fileinput({
    showPreview: true,
    showUpload: true,
    maxFileCount: 9,
    uploadLabel: "上传", removeLabel: "移除", language: "zh",
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/admin/upload/file?type=Store&f=photos',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidgphotos").val($("#hidgphotos").val().length == 0 ? data.response.data : $("#hidgphotos").val() + ";" + data.response.data);
})

function gtypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "实物产品";
    else if (value == 2)
        return "虚拟产品";
    else if (value == 3)
        return "到店服务";
    else if (value == 4)
        return "上门服务";
    else return ""
}

function typeFormatter(value, row, index) {

    if (value == undefined || value == "") return "";

    var state = row.state;
    var type = row.type;
    var typeName = "";

    $("#type option").each(function (index, item) {
        if (item.value == type) typeName = item.text
    })
    console.log("===========typeName")
    console.log(typeName)
    return typeName
}

function changeState(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/store/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {
        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/store/list'});
                } else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

function sjFormatter(value, row, index) {
    if (value == undefined || value == "") return "已下架";
    else if (value == 1)
        return "已上架";
    else return "已下架";
}

function changeGState(id, state) {
    $.ajax({
        type: 'POST',
        url: "/admin/goods/changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/store/list'});
        },
        dataType: "json"
    });
}

function deleteGOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {
        $.ajax({
            type: 'POST',
            url: "/admin/goods/deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/store/list'});
                } else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

var editsw = [];
/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var index = button.data('index');
    var modal = $(this);
    var data = $('#mytable').bootstrapTable("getData")[index]
    console.log(data)
    if (!data) return;
    modal.find('#id').val(data.id)
    modal.find('#name').val(data.name)
    modal.find('#activity').val(data.activity)
    modal.find('#latitude').val(data.latitude)
    modal.find('#longitude').val(data.longitude)
    modal.find('#paixu').val(data.paixu)
    modal.find('#address').val(data.address)
    modal.find('#characteristic').val(data.characteristic)
    modal.find('#provinceId').val(data.provinceId)
    modal.find('#province').val(data.province)
    modal.find('#cityId').val(data.cityId)
    modal.find('#city').val(data.city)
    modal.find('#districtId').val(data.districtId)
    modal.find('#district').val(data.district)

    modal.find('#price').val(data.price)
    modal.find('#pjprice').val(data.pjprice)

    modal.find('#qisongfee').val(data.qisongfee)
    modal.find('#peisongfee').val(data.peisongfee)
    modal.find('#zhutui').val(data.zhutui)
    modal.find('#xuzhi').val(data.xuzhi)
    modal.find('#info').val(data.info)

    modal.find('#leagueid').val(data.leagueid)

    $('#sninfo2').code(data.info);
    $('#info2').val(data.info);
    modal.find('#linkman').val(data.linkman)
    modal.find('#phone').val(data.phone)
    modal.find('#phone2').val(data.phone2)
    // 此部分需要注意：如果页面有两个以上的图片编辑
    // 该部分逻辑需要重写

    modal.find('#type').find("option[value='" + data['type'] + "']").attr("selected", true);
    modal.find('#jffanbi').find("option[value='" + data['jffanbi'] + "']").attr("selected", true);

    editsw["state"] == undefined ? editsw["state"] = new Switchery(document.querySelector('#state'), {color: "#1AB394"}) : ""
    if (data.state == 1) {
        setSwitchery(editsw["state"], true);
    } else {
        setSwitchery(editsw["state"], false);
    }
    modal.find('#openHour').val(data.openHour)
    modal.find('#wxno').val(data.wxno)

    editsw["isWifi"] == undefined ? editsw["isWifi"] = new Switchery(document.querySelector('#isWifi'), {color: "#1AB394"}) : ""
    if (data.isWifi == 1) {
        setSwitchery(editsw["isWifi"], true);
    } else {
        setSwitchery(editsw["isWifi"], false);
    }
    editsw["isWxpay"] == undefined ? editsw["isWxpay"] = new Switchery(document.querySelector('#isWxpay'), {color: "#1AB394"}) : ""
    if (data.isWxpay == 1) {
        setSwitchery(editsw["isWxpay"], true);
    } else {
        setSwitchery(editsw["isWxpay"], false);
    }
    editsw["isZfbpay"] == undefined ? editsw["isZfbpay"] = new Switchery(document.querySelector('#isZfbpay'), {color: "#1AB394"}) : ""
    if (data.isZfbpay == 1) {
        setSwitchery(editsw["isZfbpay"], true);
    } else {
        setSwitchery(editsw["isZfbpay"], false);
    }
    editsw["isParking"] == undefined ? editsw["isParking"] = new Switchery(document.querySelector('#isParking'), {color: "#1AB394"}) : ""
    if (data.isParking == 1) {
        setSwitchery(editsw["isParking"], true);
    } else {
        setSwitchery(editsw["isParking"], false);
    }
    editsw["isLiansuo"] == undefined ? editsw["isLiansuo"] = new Switchery(document.querySelector('#isLiansuo'), {color: "#1AB394"}) : ""
    if (data.isParking == 1) {
        setSwitchery(editsw["isLiansuo"], true);
    } else {
        setSwitchery(editsw["isLiansuo"], false);
    }
    editsw["isWaimai"] == undefined ? editsw["isWaimai"] = new Switchery(document.querySelector('#isWaimai'), {color: "#1AB394"}) : ""
    if (data.isParking == 1) {
        setSwitchery(editsw["isWaimai"], true);
    } else {
        setSwitchery(editsw["isWaimai"], false);
    }

    $("#mfilegzhimg2").fileinput('destroy');
    $("#mfilegzhimg2").fileinput({
        showPreview: true,
        showUpload: true,
        append: true, overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传", removeLabel: "移除",
        language: "zh",
        maxFileCount: 1,
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.gzhimg),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.gzhimg, 'Store', "gzhimg", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=gzhimg',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        console.log(data);
        $("#hidgzhimg2").val(data.response.data);
    }).on('filedeleted', function (event, key, jqXHR, data) {
        console.log('Key = ' + key);
        $("#hidgzhimg2").val('');
    })
    $("#hidgzhimg2").val(data.gzhimg)


    $("#mfilekefuimg2").fileinput('destroy');
    $("#mfilekefuimg2").fileinput({
        showPreview: true,
        showUpload: true,
        append: true, overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传", removeLabel: "移除",
        language: "zh",
        maxFileCount: 1,
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.kefuimg),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.kefuimg, 'Store', "kefuimg", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=kefuimg',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        console.log(data);
        $("#hidkefuimg2").val(data.response.data);
    }).on('filedeleted', function (event, key, jqXHR, data) {
        console.log('Key = ' + key);
        $("#hidkefuimg2").val('');
    })
    $("#hidkefuimg2").val(data.kefuimg)

    $("#mfilelogo2").fileinput('destroy');
    $("#mfilelogo2").fileinput({
        showPreview: true,
        showUpload: true,
        append: true, overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传", removeLabel: "移除",
        language: "zh",
        maxFileCount: 1,
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.logo),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.logo, 'Store', "logo", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=logo',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        console.log(data);
        $("#hidlogo2").val(data.response.data);
    }).on('filedeleted', function (event, key, jqXHR, data) {
        console.log('Key = ' + key);
        $("#hidlogo2").val('');
    })
    $("#hidlogo2").val(data.logo)

    $("#mfilezhizhao2").fileinput('destroy');
    $("#mfilezhizhao2").fileinput({
        showPreview: true,
        showUpload: true,
        append: true,
        overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传",
        removeLabel: "移除",
        language: "zh",
        maxFileCount: 1,
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.zhizhao),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.zhizhao, 'Store', "zhizhao", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=zhizhao',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        console.log(data);
        $("#hidzhizhao2").val(data.response.data);
    }).on('filedeleted', function (event, key, jqXHR, data) {
        console.log('Key = ' + key);
        $("#hidzhizhao2").val('');
    })
    $("#hidzhizhao2").val(data.zhizhao)

    $("#mfilexukezheng2").fileinput('destroy');
    $("#mfilexukezheng2").fileinput({
        showPreview: true,
        showUpload: true,
        append: true,
        overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传",
        removeLabel: "移除",
        language: "zh",
        maxFileCount: 1,
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.xukezheng),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.xukezheng, 'Store', "xukezheng", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=xukezheng',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        console.log(data);
        $("#hidxukezheng2").val(data.response.data);
    }).on('filedeleted', function (event, key, jqXHR, data) {
        console.log('Key = ' + key);
        $("#hidxukezheng2").val('');
    })
    $("#hidxukezheng2").val(data.xukezheng)

    $("#mfilepic2").fileinput('destroy');
    $("#mfilepic2").fileinput({
        showPreview: true,
        showUpload: true,
        append: true,
        overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传",
        removeLabel: "移除",
        language: "zh",
        maxFileCount: 1,
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.pic),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.pic, 'Store', "pic", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=pic',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        console.log(data);
        $("#hidpic2").val(data.response.data);
    }).on('filedeleted', function (event, key, jqXHR, data) {
        console.log('Key = ' + key);
        $("#hidpic2").val('');
    })
    $("#hidpic2").val(data.pic)

    $("#mfilephotos2").fileinput('destroy');
    $("#mfilephotos2").fileinput({
        showPreview: false,
        showUpload: true,
        append: true,
        overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传", removeLabel: "移除",
        language: "zh",
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.photos),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.photos, 'Store', "photos", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=photos',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        $("#hidphotos2").val($("#hidphotos2").val().length == 0 ? data.response.data : $("#hidphotos2").val() + ";" + data.response.data);
        photos2 = createsort('photos2')
    }).on("filebatchuploadsuccess", function (event, data, previewId, index) {
        $("#hidphotos2").val($("#hidphotos2").val().length == 0 ? data.response.data : $("#hidphotos2").val() + ";" + data.response.data);
        photos2 = createsort('photos2')
    }).on('filedeleted', function (event, key, jqXHR, data) {
        $("#hidphotos2").val(filedeletedbak($("#hidphotos2").val(), $("#hidphotos2bak").val(), key));
        photos2 = createsort('photos2')
    })
    $("#hidphotos2").val(data.photos)
    $("#hidphotos2bak").val(data.photos)
    photos2 = createsort('photos2')

    $("#mfilexiangqing2").fileinput('destroy');
    $("#mfilexiangqing2").fileinput({
        showPreview: false,
        showUpload: true,
        append: true,
        overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传", removeLabel: "移除",
        language: "zh",
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.xiangqing),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.xiangqing, 'Store', "photos", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=xiangqing',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        $("#hidxiangqing2").val($("#hidxiangqing2").val().length == 0 ? data.response.data : $("#hidxiangqing2").val() + ";" + data.response.data);
        xiangqing = createsort('xiangqing')
    }).on("filebatchuploadsuccess", function (event, data, previewId, index) {
        $("#hidxiangqing2").val($("#hidxiangqing2").val().length == 0 ? data.response.data : $("#hidxiangqing2").val() + ";" + data.response.data);
        xiangqing = createsort('xiangqing')
    }).on('filedeleted', function (event, key, jqXHR, data) {
        $("#hidxiangqing2").val(filedeletedbak($("#hidxiangqing2").val(), $("#hidxiangqing2bak").val(), key));
        xiangqing = createsort('xiangqing')
    })

    $("#hidxiangqing2").val(data.xiangqing)
    $("#hidxiangqing2bak").val(data.xiangqing)
    xiangqing2 = createsort('xiangqing2')

    $("#mfilezhengshu2").fileinput('destroy');
    $("#mfilezhengshu2").fileinput({
        showPreview: false,
        showUpload: true,
        append: true,
        overwriteInitial: false,
        uploadClass: "btn btn-info",
        uploadLabel: "上传", removeLabel: "移除",
        language: "zh",
        uploadIcon: "<i class=\"glyphicon glyphicon-upload\"></i> ",
        initialPreviewShowDelete: true,
        initialPreviewAsData: true,
        initialPreviewFileType: 'image',
        initialPreview: getPreviewList(data.zhengshu),
        previewFileIcon: '<i class="fa fa-file"></i>',
        allowedPreviewTypes: ['image'],
        initialPreviewConfig: getPreviewConfig(data.zhengshu, 'Store', "zhengshu", data.id),
        elErrorContainer: '#kartik-file-errors',
        uploadUrl: '/admin/upload/file?type=Store&f=zhengshu',
        allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
    }).on("fileuploaded", function (event, data) {
        $("#hidzhengshu2").val($("#hidzhengshu2").val().length == 0 ? data.response.data : $("#hidzhengshu2").val() + ";" + data.response.data);
        zhengshu2 = createsort('zhengshu2')
    }).on("filebatchuploadsuccess", function (event, data, previewId, index) {
        $("#hidzhengshu2").val($("#hidzhengshu2").val().length == 0 ? data.response.data : $("#hidzhengshu2").val() + ";" + data.response.data);
        zhengshu2 = createsort('zhengshu2')
    }).on('filedeleted', function (event, key, jqXHR, data) {
        $("#hidzhengshu2").val(filedeletedbak($("#hidzhengshu2").val(), $("#hidzhengshu2bak").val(), key));
        zhengshu2 = createsort('zhengshu2')
    })

    $("#hidzhengshu2").val(data.zhengshu)
    $("#hidzhengshu2bak").val(data.zhengshu)
    zhengshu2 = createsort('zhengshu2')

})

//tableHeight函数
function tableHeight() {
    //可以根据自己页面情况进行调整
    return $(window).height() - 200;
}

function adddhgoods(sid) {
    console.log("======adddhgoods sid=" + sid)
    $('#hidsid').val(sid)
    $('#modal-agoods-form').modal("show");
}

function setauth(sid, index) {
    console.log("======sid=" + sid)
    var data = $('#mytable').bootstrapTable("getData")[index]
    console.log("======data=" + data)
    console.log(data)

    $('#yzno').val(data.yzno)
    $('#gxbno').val(data.gxbno)
    $('#authno').val(data.authno)

    $('#hidid').val(sid)
    var isYikatong = data['isYikatong'] && data['isYikatong'] == 1 ? 1 : 0;
    var isDulizs = data['isDulizs'] && data['isDulizs'] == 1 ? 1 : 0;

    $('#yzstatus').find("option[value='" + data.yzstatus + "']").attr("selected", true);
    $('#isYikatong').find("option[value='" + isYikatong + "']").attr("selected", true);
    $('#isDulizs').find("option[value='" + isDulizs + "']").attr("selected", true);

    $('#hidid').val(sid)
    $('#modal-setauth-form').modal("show");
}

function guanlian(sid) {
    console.log("===id=" + sid)
    swal({
        title: "关联店家店铺",
        text: "",
        type: "info",
        input: "text",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        animation: "slide-from-top",
        inputPlaceholder: "填写商家店铺注册手机号"
    }).then(function (res) {
        // var re = /^[1-9]+[0-9]*]*$/;
        // if(!re.test(inputValue)){
        if (res.length == 0) {
            swal("请填写商家店铺注册手机号");
            return
        }
        console.log("======res=" + res)
        $.ajax({
            type: 'POST',
            url: "guanlian",
            data: {
                sid: sid,
                phone: res,
            },
            success: function (e) {
                if (e.code == 0) {
                    swal({title: "操作成功", text: e.info, type: "success"})
                } else {
                    swal({title: "操作失败", text: e.msg, type: "error"})
                }
            },
            dataType: "json"
        });
    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

function initmpwd(id) {
    swal({
        title: "设置商家账户密码",
        text: "",
        type: "info",
        input: "text",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        animation: "slide-from-top",
        inputPlaceholder: "输入该门诊商家账号手机号"
    }).then(function (res) {
        if (res.length == 0) {
            swal("手机号不能为空");
            return;
        }
        console.log("======res=" + res)
        $.ajax({
            type: 'POST',
            url: "initmchpwd",
            data: {
                id: id,
                phone: res,
            },
            success: function (e) {
                if (e.code == 0) {
                    swal({title: "操作成功", text: e.info, type: "success"})
                } else {
                    swal({title: "操作失败", text: e.msg, type: "error"})
                }
            },
            dataType: "json"
        });
    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

function copymapp(id) {
    var path = "pages/myymt/myymt.html?sid=" + id
    console.log(path)
    var tag = document.createElement('input');
    tag.setAttribute('id', 'cp_hgz_input');
    tag.value = path;
    document.getElementsByTagName('body')[0].appendChild(tag);
    document.getElementById('cp_hgz_input').select();
    document.execCommand('copy');
    document.getElementById('cp_hgz_input').remove();
    swal({title: '您已成功复制商家健康码路径！', text: '', timer: 2000})
}

$(function () {
    var sw = [];
    $(".js-switch").each(function (i, e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })
    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })

    $("#sninfo").summernote({
        lang: "zh-CN",
        minHeight: 350,
        dialogsFade: true,// Add fade effect on dialogs
        dialogsInBody: true,// Dialogs can be placed in body, not in summernote
        disableDragAndDrop: false,// default false You can disable drag and drop
        onImageUpload: function (files, editor, welEditable) {
            console.log(">>>>>>>>onImageUpload start")
            // uploadFile(files[0],editor,welEditable,"image");
            for (var i = 0; i < files.length; i++) {
                uploadFile(files[i], editor, welEditable, "image");
            }
        },
        onFileUpload: function (files, editor, welEditable) {
            console.log(">>>>>>>>onFileUpload start")
            uploadFile(files[0], editor, welEditable, "file");
        }
    })
    $("#sninfo2").summernote({
        lang: "zh-CN",
        minHeight: 350,
        dialogsFade: true,// Add fade effect on dialogs
        dialogsInBody: true,// Dialogs can be placed in body, not in summernote
        disableDragAndDrop: false,// default false You can disable drag and drop
        onImageUpload: function (files, editor, welEditable) {
            console.log(">>>>>>>>onImageUpload start")
            // uploadFile(files[0],editor,welEditable,"image");
            for (var i = 0; i < files.length; i++) {
                uploadFile(files[i], editor, welEditable, "image");
            }
        },
        onFileUpload: function (files, editor, welEditable) {
            console.log(">>>>>>>>onFileUpload start")
            uploadFile(files[0], editor, welEditable, "file");
        }
    })

    function actlFormatter(value, row, index) {
        var state = row.state;
        var id = row.id;
        var isDuihuan = $("#hidIsDuihuan").val()
        var version = $('#hidversion').val();

        var res = '<button data-toggle="modal" class="btn-sm btn-primary" href="list.html#modal-form" data-row=' + id + ' data-index=' + index + '>修改</button>';
        res += ' <button type="button" class="btn-sm btn-info" onclick="guanlian(\'' + id + '\')" >关联店家</button>';

        if (isDuihuan == 1) res += ' <button type="button" class="btn-sm btn-info" onclick="adddhgoods(\'' + id + '\')" >添加兑换商品</button>';
        if (version == 4) {
            res += ' <button typ-e="button" class="btn-sm btn-danger" onclick="setauth(\'' + id + '\',\'' + index + '\')">认证商家</button>';
            res += ' <button typ-e="button" class="btn-sm btn-info" onclick="initmpwd(\'' + id + '\')" >设置商户账户密码</button>';
            res += ' <button typ-e="button" class="btn-sm btn-info" onclick="copymapp(\'' + id + '\')" >复制一码通路径</button>';
        }

        if (state == undefined) {
        } else if (state == 1)
            res += ' <button type="button" class="btn-sm btn-info" onclick="changeState(\'' + id + '\',-1)" >禁用</button>';
        else
            res += ' <button type="button" class="btn-sm btn-info" onclick="changeState(\'' + id + '\',1)" >启用</button>';
        res += ' <button type="button" class="btn-sm btn-danger" onclick="deleteOne(\'' + id + '\')">删除</button>';
        return res;
    }

    function sjactFormatter(value, row, index) {
        var state = row.state;
        var id = row.id;
        var isNav = row.isNav;
        var type = row.type;
        var id = row.id;

        // var res = '<button data-toggle="modal" class="btn-sm btn-primary" href="#modal-mgoods-form" ' +
        //     'data-row=' + id + ' data-index=' + index + '>修改</button>';
        var res = '';
        if (state == undefined) {
        } else if (state == 1)
            res += ' <button type="button" class="btn-sm btn-info" onclick="changeGState(\'' + id + '\',-1)" >下架</button>';
        else
            res += ' <button typ-e="button" class="btn-sm btn-info" onclick="changeGState(\'' + id + '\',1)" >上架</button>';
        res += ' <button typ-e="button" class="btn-sm btn-danger" onclick="deleteGOne(\'' + id + '\')">删除</button>';

        return res;
    }

    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url: "/admin/store/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination: true,//是否分页
        queryParamsType: 'limit',
        queryParams: queryParams,
        sidePagination: 'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize: 50,//单页记录数
        pageList: [50, 100, 200, 500],//分页步进值
        showExport: true,//显示导出
        showRefresh: false,//刷新按钮
        showColumns: false,
        detailView: true,//父子表
        onExpandRow: function (index, row, $detail) {
            initSubTable(index, row, $detail);
        },
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns: [
            {
                title: '全选',
                field: 'select',
                checkbox: true,
                width: 25,
                align: 'center',
                valign: 'middle'
            },
            {
                title: 'ID',
                field: 'id',
                visible: false
            },
            {
                title: '名称',
                field: 'name'
            },
            // {
            //     title:'地址',
            //     field:'address'
            // },
            // {
            //     title: '商家特色',
            //     field: 'characteristic'
            // },
            {
                title: '省',
                field: 'province'
            },
            {
                title: '市',
                field: 'city'
            },
            {
                title: '区',
                field: 'district'
            },
            {
                title: '商圈',
                field: 'areaName'
            },
            {
                title: '联系人',
                field: 'linkman'
            },
            {
                title: '联系电话',
                field: 'phone'
            },
            {
                title: '店铺图片',
                field: 'pic',
                formatter: "imgFormatter"
            },
            {
                title: '商家类目',
                field: 'type',
                formatter: "typeFormatter"
            },
            // {
            //     title: '营业时间',
            //     field: 'openHour'
            // },
            {
                title: '微信号',
                field: 'wxno'
            },
            // {
            //     title: '支持WIFI',
            //     field: 'isWifi',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '支持微信支付',
            //     field: 'isWxpay',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '支持支付宝',
            //     field: 'isZfbpay',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '支持停车位',
            //     field: 'isParking',
            //     formatter: "sfFormatter"
            // },
            {
                title: '启用状态',
                field: 'state',
                formatter: "stateFormatter"
            },
            {
                title: '操作',
                field: 'Attribute',
                align: 'center',
                width: 320,
                formatter: actlFormatter
            }
        ],
        locale: 'zh-CN',//中文支持,
    })

    //请求服务数据时所传参数
    function initSubTable(index, row, $detail) {
        console.log(index)
        console.log(row)
        console.log($detail)
        $("hidsid2").val(row.id)
        var sid = row.id;
        console.log("======sid=" + sid)

        var cur_table = $detail.html('<table></table>').find('table');
        $(cur_table).bootstrapTable({
            url: "/admin/store/goodslist",
            method: 'get',
            queryParams: {sid: sid, offset: 0, limit: 20},
            // ajaxOptions: {sid: sid},
            clickToSelect: true,
            detailView: false,//父子表
            uniqueId: "id",
            dataField: "records",
            queryParamsType: 'limit',
            locale: 'zh-CN',//中文支持,
            sidePagination: 'server',
            pagination: false,//是否分页
            columns: [{
                checkbox: false
            }, {
                title: 'ID',
                field: 'id',
                visible: false
            },
                {
                    title: '商品名称',
                    field: 'name'
                },
                {
                    title: '商品原价',
                    field: 'price'
                },
                {
                    title: '实际售价',
                    field: 'saleprice'
                },
                {
                    title: '积分兑换',
                    field: 'jifen'
                },
                {
                    title: '商品主图',
                    field: 'pic',
                    formatter: "imgFormatter"
                },
                {
                    title: '总库存数',
                    field: 'stores'
                },
                {
                    title: '产品类型',
                    field: 'gtype',
                    formatter: "gtypeFormatter"
                },
                {
                    title: '上架状态',
                    field: 'state',
                    formatter: "sjFormatter"
                },
                {
                    title: '操作',
                    field: 'Attribute',
                    align: 'center',
                    width: 360,
                    formatter: sjactFormatter
                }
            ]
        })
    }

    //请求服务数据时所传参数
    function queryParams(params) {
        var p = {
            limit: params.limit,
            offset: params.offset,
            sort: params.sort,
            order: params.order
            , storetype: $('#storetype').val()
            , search: $('#search').val()
        }
        return p;
    }

    $('#search_btn').click(function () {
        var p = {
            search: $('#search').val(),
            ts: new Date().getTime()
        };
        $('#mytable').bootstrapTable('refresh', p);
    })

    /*
     * 验证错误提示信息高亮
     */
    $.validator.setDefaults({
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        }, errorElement: "span", errorPlacement: function (e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
    }), $("#aform").validate(), $("#mform").validate();
})

function checkAdd() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name") + "=" + $(this).val())
        }
    });

    if ($("#hidpic").attr("required") == "required" && $("#hidpic").val() == "") {
        swal({title: "请上传图片", text: "", type: "warning"})
        return false;
    }
    return true;
}

function checkModify() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id") + "=" + $(this).val())
        }
    });

    $('#info2').val($('#sninfo2').code())
    if ($("#hidpic2").attr("required") == "required"
        && $("#hidpic2").val() == "") {
        swal({title: "请上传图片", text: "", type: "warning"})
        return false;
    }
    return true;
}

function checkAgoodsModify() {

    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name") + "=" + $(this).val())
        }
    });
    var xg = $("#xiangou2").val();

    if (xg == "无") {
        $("#xiangou2").val(0)
    }
    if (!$.num(xg, true)) {
        swal({title: "请填写整数", text: "", type: "warning"})
        return false;
    }

    if ($("#hidpic").attr("required") == "required"
        && $("#hidpic").val() == "") {
        swal({title: "请上传图片", text: "", type: "warning"})
        return false;
    }

    if ($("#hidinfo").attr("required") == "required"
        && $("#hidinfo").val() == "") {
        swal({title: "请上传商品详情图", text: "", type: "warning"})
        return false;
    }

    console.log($('#info').val())
    return true;

    return true;
}

function checkMgoodsModify() {

    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id") + "=" + $(this).val())
        }
    });

    if ($("#hidgphotos2").attr("required") == "required"
        && $("#hidgphotos2").val() == "") {
        swal({title: "请上传图片", text: "", type: "warning"})
        return false;
    }

    return true;
}